﻿Public Partial Class frm_OtherServices
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "OTHER SERVICE "
            Dim dt As New DataTable
            dt = SetStructure(dt)
            ViewState("dtMat") = dt
            Dim ds As New DataSet
            ds.Tables.Add(dt.Copy)
            Session("MAT_USE") = ds
            Session("PORT_PRODUCT") = Nothing
            gridTariff.Columns(0).Visible = False
        End If
    End Sub


    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub LoadREcord()
        Dim clsGet As New ClassFunctionGetData
        ViewState("Record") = clsGet.Get_OtherServiceRecord()
        gridRecord.PageIndex = 0
        gridRecord.DataSource = ViewState("Record")
        gridRecord.DataBind()
    End Sub

    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridRecord.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridRecord.PageIndex = ddlPage.SelectedIndex
        gridRecord.DataSource = ViewState("Record")
        gridRecord.DataBind()
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridRecord.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridRecord.PageIndex = 0
            Case "prev"
                If (gridRecord.PageIndex <> 0) Then
                    gridRecord.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridRecord.PageIndex <> gridRecord.PageCount) Then
                    gridRecord.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridRecord.PageIndex = gridRecord.PageCount
        End Select
        gridRecord.DataSource = ViewState("Record")
        gridRecord.DataBind()
    End Sub

    Private Sub txtCustomer_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCustomer.TextChanged
        Dim dt As DataSet = clsGet.Get_CustomerMaster(txtCustomer.Text.Split("-")(0).Trim())
        If (dt.Tables(0).Rows.Count = 1) Then
            txtCustomer.Text = dt.Tables(0).Rows(0).Item("Customer_no") & "-" & dt.Tables(0).Rows(0).Item("Name")
            rowAddress.Visible = True
            Dim Address As String = ""
            If Not (dt.Tables(0).Rows(0).Item("STREET").ToString().Trim() = "") Then
                txtStreet.Text = dt.Tables(0).Rows(0).Item("STREET").ToString().Trim()
            End If
            If Not (dt.Tables(0).Rows(0).Item("STR_SUPPL3").ToString().Trim() = "") Then
                txtSup.Text = dt.Tables(0).Rows(0).Item("STR_SUPPL3").ToString().Trim()
            End If
            If Not (dt.Tables(0).Rows(0).Item("LOCATION").ToString().Trim() = "") Then
                txtLocation.Text = dt.Tables(0).Rows(0).Item("LOCATION").ToString().Trim()
            End If
            If Not (dt.Tables(0).Rows(0).Item("District").ToString().Trim() = "") Then
                txtDistrict.Text = dt.Tables(0).Rows(0).Item("District").ToString().Trim()
            End If
            If Not (dt.Tables(0).Rows(0).Item("City").ToString().Trim() = "") Then
                txtCity.Text = dt.Tables(0).Rows(0).Item("City").ToString().Trim()
            End If
            If Not (dt.Tables(0).Rows(0).Item("Postal_Code").ToString().Trim() = "") Then
                txtPostal.Text = dt.Tables(0).Rows(0).Item("Postal_Code").ToString().Trim()
            End If
            txtStreet.Enabled = False
            txtSup.Enabled = False
            txtCity.Enabled = False
            txtLocation.Enabled = False
            txtDistrict.Enabled = False
            txtPostal.Enabled = False
            Dim dtCus As DataTable = clsGet.Get_BillTo_For_Other(txtCustomer.Text.Split("-")(0).Trim())
            If (dtCus.Rows.Count > 1) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " document.getElementById('ctl00_contentplaceholder1_TabPayment_TabOtherService_iframeBillTO').src = '../FormPopup/BillTo_Popup.aspx';", True)
                Session("CUSTOMER_SOLD_TO") = txtCustomer.Text.Split("-")(0).Trim()
                ModalPopupExtender3.Show()
            Else
                lbBillTO.Text = txtCustomer.Text
            End If
        Else
            rowAddress.Visible = False
            lbBillTO.Text = String.Empty
            rowBillTo.Visible = False
        End If
    End Sub

    Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Dim dtMat As DataTable = ViewState("dtMat")
        Dim ds As DataSet = Session("MAT_USE")
        For Each dr As DataRow In ds.Tables(0).Rows
            If (dr.Item("MATERIAL") & "" <> "") Then
                Dim drN As DataRow = dtMat.NewRow()
                drN.Item("MATERIAL") = dr.Item("MATERIAL")
                drN.Item("Text_line") = dr.Item("Text_line")
                drN.Item("Unit") = dr.Item("Unit")
                drN.Item("Count") = dr.Item("Count")
                drN.Item("MAT_Default") = dr.Item("MAT_Default")
                drN.Item("KBETR") = dr.Item("KBETR")
                drN.Item("KONWA") = dr.Item("KONWA")
                drN.Item("MAT_Relation") = dr.Item("MAT_Relation")
                drN.Item("data1") = ""
                drN.Item("data2") = dr.Item("KBETR")
                drN.Item("data3") = dr.Item("Count")
                drN.Item("data4") = (CDbl(dr.Item("KBETR")) * CDbl(dr.Item("Count"))).ToString("################0.00")
                drN.Item("New_Old") = "N"
                drN.Item("Owner") = ""
                dtMat.Rows.Add(drN)
                dtMat.AcceptChanges()
            End If

        Next
        gridTariff.PageIndex = 0
        gridTariff.DataSource = dtMat
        gridTariff.DataBind()
        ViewState("dtMat") = dtMat
        Dim dt As New DataTable
        dt = SetStructure(dt)
        ds = New DataSet
        ds.Tables.Add(dt.Copy)
        Session("MAT_USE") = ds
    End Sub

    Private Function SetStructure(ByVal dv As DataTable) As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("MATERIAL", GetType(String))
        dt.Columns.Add("Booking_no", GetType(String))
        dt.Columns.Add("Text_line", GetType(String))
        dt.Columns.Add("Unit", GetType(String))
        dt.Columns.Add("Count", GetType(String))
        dt.Columns.Add("MAT_Default", GetType(String))
        dt.Columns.Add("KBETR", GetType(Decimal))
        dt.Columns.Add("KONWA", GetType(String))
        dt.Columns.Add("MAT_Relation", GetType(String))
        dt.Columns.Add("MAT_Compute", GetType(String))
        dt.Columns.Add("MAT_Free", GetType(String))
        dt.Columns.Add("data1", GetType(String))
        dt.Columns.Add("data2", GetType(String))
        dt.Columns.Add("data3", GetType(String))
        dt.Columns.Add("data4", GetType(String))
        dt.Columns.Add("New_Old", GetType(String))
        dt.Columns.Add("Owner", GetType(String))
        dt.AcceptChanges()

        Return dt
    End Function

    Private Sub gridTariff_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridTariff.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridTariff.PageIndex <> 0) Then
            Index = Index Mod (gridTariff.PageIndex * 10)
        End If
        If e.CommandName = "cmddelete" Then
            GetDataTariff()
            Dim dtMat As DataTable = ViewState("dtMat")
            dtMat.Rows(Index).Delete()
            dtMat.AcceptChanges()
            gridTariff.DataSource = dtMat
            gridTariff.DataBind()
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ลบข้อมูลเรียบร้อย!!');", True)
        End If
    End Sub

    Private Sub GetDataTariff()
        Dim dtMat As DataTable = ViewState("dtMat")
        For i As Integer = 0 To gridTariff.Rows.Count - 1
            Dim txtQTY As TextBox = CType(gridTariff.Rows(i).FindControl("txtQTY"), TextBox)
            dtMat.Rows(i).Item("count") = txtQTY.Text
            dtMat.AcceptChanges()
        Next
        ViewState("dtMat") = dtMat
    End Sub

    Private Sub gridTariff_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridTariff.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            If (e.Row.RowIndex = 0) Then
                ViewState("VAT") = 0
                ViewState("SUM") = 0
                ViewState("WHT") = 0
            End If
            Dim txtQTY As TextBox = CType(e.Row.FindControl("txtQTY"), TextBox)
            Try
                e.Row.Cells(5).Text = FormatNumber(CDbl(txtQTY.Text) * CDbl(e.Row.Cells(3).Text), 2)

                Dim Vat As String = ""
                Dim dsConfig As DataSet = Session("Config")
                If (dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(1) = "00") Then
                    Vat = CInt(dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(0)).ToString()
                Else
                    Vat = dsConfig.Tables(0).Rows(0).Item("Vat").ToString()
                End If
                Dim dt As DataTable = clsGet.Get_MeterailWhithodingTax(e.Row.Cells(1).Text)
                If (dt.Rows.Count > 0) Then
                    If (dt.Rows(0).Item("MAT_WHT") & "" = "01") Or (dt.Rows(0).Item("MAT_WHT") & "" = "02") Or (dt.Rows(0).Item("MAT_WHT") & "" = "03") Then
                        '1%
                        ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(e.Row.Cells(5).Text) * 1) / 100)))
                    ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "04") Or (dt.Rows(0).Item("MAT_WHT") & "" = "05") Then
                        '3%
                        ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(e.Row.Cells(5).Text) * 3) / 100)))
                    ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "06") Or (dt.Rows(0).Item("MAT_WHT") & "" = "22") Then
                        '5%
                        ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(e.Row.Cells(5).Text) * 5) / 100)))
                    End If
                End If
                ViewState("SUM") = CDbl(ViewState("SUM")) + CDbl(e.Row.Cells(5).Text)
                ViewState("VAT") = (CDbl(ViewState("VAT")) + ((CDbl(e.Row.Cells(5).Text) * CDbl(Vat)) / 100))

                If Not (chkWHT.Checked) Then
                    lbSum.Text = FormatNumber(ViewState("SUM"), 2)
                    lbVat.Text = FormatNumber(ViewState("VAT"), 2)
                    lbWHT.Text = "0"
                    lbNET.Text = FormatNumber(CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))), 2)
                Else
                    lbSum.Text = FormatNumber(ViewState("SUM"), 2)
                    lbVat.Text = FormatNumber(ViewState("VAT"), 2)
                    lbWHT.Text = FormatNumber(ViewState("WHT"), 2)
                    lbNET.Text = FormatNumber((CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))) - CDbl(ViewState("WHT"))), 2)
                End If
            Catch ex As Exception

            End Try
        End If
    End Sub

    Public Sub chkX_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        ViewState("VAT") = 0
        ViewState("SUM") = 0
        ViewState("WHT") = 0
        For i As Integer = 0 To gridTariff.Rows.Count - 1
            Dim chk As CheckBox = CType(gridTariff.Rows(i).FindControl("chk"), CheckBox)
            If Not (gridTariff.Columns(0).Visible) Then
                chk.Checked = True
            End If
            If (chk.Checked) Then
                Try
                    Dim Vat As String = ""
                    Dim dsConfig As DataSet = Session("Config")
                    If (dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(1) = "00") Then
                        Vat = CInt(dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(0)).ToString()
                    Else
                        Vat = dsConfig.Tables(0).Rows(0).Item("Vat").ToString()
                    End If
                    Dim dt As DataTable = clsGet.Get_MeterailWhithodingTax(gridTariff.Rows(i).Cells(1).Text)
                    If (dt.Rows.Count > 0) Then
                        If (dt.Rows(0).Item("MAT_WHT") & "" = "01") Or (dt.Rows(0).Item("MAT_WHT") & "" = "02") Or (dt.Rows(0).Item("MAT_WHT") & "" = "03") Then
                            '1%
                            ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(gridTariff.Rows(i).Cells(5).Text) * 1) / 100)))
                        ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "04") Or (dt.Rows(0).Item("MAT_WHT") & "" = "05") Then
                            '3%
                            ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(gridTariff.Rows(i).Cells(5).Text) * 3) / 100)))
                        ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "06") Or (dt.Rows(0).Item("MAT_WHT") & "" = "22") Then
                            '5%
                            ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(gridTariff.Rows(i).Cells(5).Text) * 5) / 100)))
                        End If
                    End If
                    ViewState("SUM") = CDbl(ViewState("SUM")) + CDbl(gridTariff.Rows(i).Cells(5).Text)
                    ViewState("VAT") = (CDbl(ViewState("VAT")) + ((CDbl(gridTariff.Rows(i).Cells(5).Text) * CDbl(Vat)) / 100))

                Catch ex As Exception

                End Try
            End If

        Next
        If Not (CheckBox1.Checked) Then
            lbSum.Text = FormatNumber(ViewState("SUM"), 2)
            lbVat.Text = FormatNumber(ViewState("VAT"), 2)
            lbWHT.Text = "0"
            lbNET.Text = FormatNumber(CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))), 2)
        Else
            lbSum.Text = FormatNumber(ViewState("SUM"), 2)
            lbVat.Text = FormatNumber(ViewState("VAT"), 2)
            lbWHT.Text = FormatNumber(ViewState("WHT"), 2)
            lbNET.Text = FormatNumber((CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))) - CDbl(ViewState("WHT"))), 2)
        End If
    End Sub

    Public Sub txtQTY_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        For i As Integer = 0 To gridTariff.Rows.Count - 1
            If (i = 0) Then
                ViewState("VAT") = 0
                ViewState("SUM") = 0
                ViewState("WHT") = 0
            End If
            Dim txtQTY As TextBox = CType(gridTariff.Rows(i).FindControl("txtQTY"), TextBox)
            Try
                gridTariff.Rows(i).Cells(4).Text = FormatNumber(CDbl(txtQTY.Text) * CDbl(gridTariff.Rows(i).Cells(2).Text), 2)
                Dim Vat As String = ""
                Dim dsConfig As DataSet = Session("Config")
                If (dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(1) = "00") Then
                    Vat = CInt(dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(0)).ToString()
                Else
                    Vat = dsConfig.Tables(0).Rows(0).Item("Vat").ToString()
                End If
                Dim dt As DataTable = clsGet.Get_MeterailWhithodingTax(gridTariff.Rows(i).Cells(0).Text)
                If (dt.Rows.Count > 0) Then
                    If (dt.Rows(0).Item("MAT_WHT") & "" = "01") Or (dt.Rows(0).Item("MAT_WHT") & "" = "02") Or (dt.Rows(0).Item("MAT_WHT") & "" = "03") Then
                        '1%
                        ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(gridTariff.Rows(i).Cells(4).Text) * 1) / 100)))
                    ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "04") Or (dt.Rows(0).Item("MAT_WHT") & "" = "05") Then
                        '3%
                        ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(gridTariff.Rows(i).Cells(4).Text) * 3) / 100)))
                    ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "06") Or (dt.Rows(0).Item("MAT_WHT") & "" = "22") Then
                        '5%
                        ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(gridTariff.Rows(i).Cells(4).Text) * 5) / 100)))
                    End If
                End If
                ViewState("SUM") = CDbl(ViewState("SUM")) + CDbl(gridTariff.Rows(i).Cells(4).Text)
                ViewState("VAT") = (CDbl(ViewState("VAT")) + ((CDbl(gridTariff.Rows(i).Cells(4).Text) * CDbl(Vat)) / 100))
            Catch ex As Exception

            End Try
        Next
        If Not (chkWHT.Checked) Then
            lbSum.Text = FormatNumber(ViewState("SUM"), 2)
            lbVat.Text = FormatNumber(ViewState("VAT"), 2)
            lbWHT.Text = "0"
            lbNET.Text = FormatNumber(CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))), 2)
        Else
            lbSum.Text = FormatNumber(ViewState("SUM"), 2)
            lbVat.Text = FormatNumber(ViewState("VAT"), 2)
            lbWHT.Text = FormatNumber(ViewState("WHT"), 2)
            lbNET.Text = FormatNumber((CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))) - CDbl(ViewState("WHT"))), 2)
        End If
    End Sub

    Private Sub btnVesselLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVesselLoad.Click
        If Not (Session("VESSEL_OTHER") Is Nothing) Then
            txtBooking.Text = Session("VESSEL_OTHER")
            Session("VESSEL_OTHER") = Nothing
        End If

    End Sub

    Private Sub btnBillTOLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBillTOLoad.Click
        lbBillTO.Text = Session("CUSTOMER_SOLD_TO") & ""
        rowBillTo.Visible = True

        Dim dt As DataSet = clsGet.Get_CustomerMaster(lbBillTO.Text.Split("-")(0).Trim())
        If (dt.Tables(0).Rows.Count = 1) Then
            rowAddress.Visible = True
            Dim Address As String = ""
            If Not (dt.Tables(0).Rows(0).Item("STREET").ToString().Trim() = "") Then
                txtStreet.Text = dt.Tables(0).Rows(0).Item("STREET").ToString().Trim()
            End If
            If Not (dt.Tables(0).Rows(0).Item("STR_SUPPL3").ToString().Trim() = "") Then
                txtSup.Text = dt.Tables(0).Rows(0).Item("STR_SUPPL3").ToString().Trim()
            End If
            If Not (dt.Tables(0).Rows(0).Item("LOCATION").ToString().Trim() = "") Then
                txtLocation.Text = dt.Tables(0).Rows(0).Item("LOCATION").ToString().Trim()
            End If
            If Not (dt.Tables(0).Rows(0).Item("District").ToString().Trim() = "") Then
                txtDistrict.Text = dt.Tables(0).Rows(0).Item("District").ToString().Trim()
            End If
            If Not (dt.Tables(0).Rows(0).Item("City").ToString().Trim() = "") Then
                txtCity.Text = dt.Tables(0).Rows(0).Item("City").ToString().Trim()
            End If
            If Not (dt.Tables(0).Rows(0).Item("Postal_Code").ToString().Trim() = "") Then
                txtPostal.Text = dt.Tables(0).Rows(0).Item("Postal_Code").ToString().Trim()
            End If
            txtStreet.Enabled = False
            txtSup.Enabled = False
            txtCity.Enabled = False
            txtLocation.Enabled = False
            txtDistrict.Enabled = False
            txtPostal.Enabled = False
        Else
            rowAddress.Visible = False
            lbBillTO.Text = String.Empty
            rowBillTo.Visible = False
        End If

        Session("CUSTOMER_SOLD_TO") = Nothing
    End Sub

    Private Sub radioCustomer_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioCustomer.SelectedIndexChanged
        If (radioCustomer.SelectedValue = "I") Then
            PanelCustomer.Visible = True
            PanelOneTIme.Visible = False
            txtCustomer.Text = txtCustomerOneTime.Text
            txtStreet.Enabled = False
            txtSup.Enabled = False
            txtCity.Enabled = False
            txtLocation.Enabled = False
            txtDistrict.Enabled = False
            txtPostal.Enabled = False
            rowAddress.Visible = False
            rowBillTo.Visible = False
        Else
            rowBillTo.Visible = False
            rowAddress.Visible = True
            PanelOneTIme.Visible = True
            PanelCustomer.Visible = False
            txtCustomerOneTime.Text = txtCustomer.Text
            txtStreet.Enabled = True
            txtSup.Enabled = True
            txtCity.Enabled = True
            txtLocation.Enabled = True
            txtDistrict.Enabled = True
            txtPostal.Enabled = True
            txtStreet.Text = String.Empty
            txtSup.Text = String.Empty
            txtCity.Text = String.Empty
            txtLocation.Text = String.Empty
            txtDistrict.Text = String.Empty
            txtPostal.Text = String.Empty
        End If
    End Sub

    Private Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        If (radioCustomer.SelectedValue = "I") Then
            If (txtCustomer.Text.Trim() = "") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุชื่อลุกค้า !!');", True)
                Exit Sub
            End If
            Dim dt As DataSet = clsGet.Get_CustomerMaster(txtCustomer.Text.Split("-")(0).Trim())
            If (dt.Tables(0).Rows.Count = 0) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่พบข้อมูลลูกค้าลุกค้า !!');", True)
                Exit Sub
            End If
        Else
            If (txtCustomerOneTime.Text.Trim() = "") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุชื่อลุกค้า !!');", True)
                Exit Sub
            End If
        End If
        If (txtDate.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุวันที่ !!');", True)
            Exit Sub
        End If
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim Customer_no As String = ""
        Dim Customer_name As String = ""
        Dim Bill_to As String = ""
        Dim BIll_to_name As String = ""
        If (radioCustomer.SelectedValue = "I") Then
            Customer_no = txtCustomer.Text.Split("-")(0)
            Dim Name() As String = txtCustomer.Text.Split("-")
            For i = 1 To Name.Length - 1
                Customer_name &= Name(i).ToString()
            Next
            Bill_to = lbBillTO.Text.Split("-")(0)
            Name = lbBillTO.Text.Split("-")
            For i = 1 To Name.Length - 1
                BIll_to_name &= Name(i).ToString()
            Next
        Else
            Customer_no = "OND001"
            Bill_to = "OND001"
            Customer_name = txtCustomerOneTime.Text
            BIll_to_name = txtCustomerOneTime.Text
        End If

        Application.Lock()
        conn.Open()
        Dim Ots_ID As String = clsDB.GeneratorID(conn, "OtherService_Head")

        tran = clsDB.BeginTran(conn)
        If (clsFc.InsertOrtherService("", Ots_ID, txtDate.Text, txtTime.Text, radioCustomer.SelectedValue, Customer_no, Customer_name, Bill_to, BIll_to_name, txtCity.Text, txtDistrict.Text, txtLocation.Text, txtPostal.Text, txtSup.Text, txtStreet.Text, txtBooking.Text.Split("-")(0), radioPORT.SelectedValue, conn, tran) > 0) Then
            For i As Integer = 0 To gridTariff.Rows.Count - 1
                Dim txtQTY As TextBox = CType(gridTariff.Rows(i).FindControl("txtQTY"), TextBox)
                If (clsFc.InsertOrtherService_ITem(Ots_ID, gridTariff.DataKeys(i).Values("MATERIAL"), gridTariff.DataKeys(i).Values("Text_line"), gridTariff.DataKeys(i).Values("data2"), txtQTY.Text, gridTariff.Rows(i).Cells(5).Text, "", conn, tran) < 1) Then
                    condition = False
                End If
            Next

        Else
            condition = False
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
            BtnPrint.Visible = True
            BtnSave.Visible = False
            ViewState("Ots_ID_NewOT") = Ots_ID
            gridTariff.Columns(0).Visible = True
            For i As Integer = 0 To gridTariff.Rows.Count - 1
                Dim txtQTY As TextBox = CType(gridTariff.Rows(i).FindControl("txtQTY"), TextBox)
                Dim chk As CheckBox = CType(gridTariff.Rows(i).FindControl("chk"), CheckBox)
                chk.Checked = True
                txtQTY.Enabled = False
            Next
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('การความผิดพลาดในการบันทึกข้อมูล !!');", True)
        End If
        conn.Close()
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub TabPayment_ActiveTabChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabPayment.ActiveTabChanged
        If (TabPayment.ActiveTabIndex = 0) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.location.reload();", True)
        Else
            LoadREcord()
        End If
    End Sub

    Private Sub chkWHT_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkWHT.CheckedChanged
        If Not (chkWHT.Checked) Then
            lbSum.Text = FormatNumber(ViewState("SUM"), 2)
            lbVat.Text = FormatNumber(ViewState("VAT"), 2)
            lbWHT.Text = "0"
            lbNET.Text = FormatNumber(CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))), 2)
        Else
            lbSum.Text = FormatNumber(ViewState("SUM"), 2)
            lbVat.Text = FormatNumber(ViewState("VAT"), 2)
            lbWHT.Text = FormatNumber(ViewState("WHT"), 2)
            lbNET.Text = FormatNumber((CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))) - CDbl(ViewState("WHT"))), 2)
        End If

    End Sub

    Private Sub BtnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnClear.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.location.reload();", True)
    End Sub

    Private Sub gridRecord_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridRecord.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridRecord.PageIndex <> 0) Then
            Index = Index Mod (gridRecord.PageIndex * 10)
        End If
        If e.CommandName = "Edit" Then
            PanelRecordOther.Visible = False
            PanelDetail.Visible = True
            ViewState("OTS_ID_PRINT") = gridRecord.DataKeys(Index).Values("Ots_ID") & ""
            BindData(gridRecord.DataKeys(Index).Values("Ots_ID") & "")
        End If
    End Sub

    Private Sub BindData(ByVal Ots_ID As String)
        Dim dt As DataTable = clsGet.Get_OtherService(Ots_ID)
        If (dt.Rows.Count > 0) Then
            lbDateTIme.Text = dt.Rows(0).Item("dayIn") & " " & dt.Rows(0).Item("TimeIn")
            lbSoldTodetail.Text = dt.Rows(0).Item("Customer_no") & "-" & dt.Rows(0).Item("Customer_name")
            If (dt.Rows(0).Item("PORT") & "" = "L") Then
                lbTerminal.Text = "Liquid"
            Else
                lbTerminal.Text = "Bulk"
            End If
            lbBIlltoDetail.Text = dt.Rows(0).Item("Bill_To_no") & "-" & dt.Rows(0).Item("Bill_To_name")
            lbCity.Text = dt.Rows(0).Item("City") & ""
            lbDistrict.Text = dt.Rows(0).Item("District") & ""
            lbStreet.Text = dt.Rows(0).Item("Street") & ""
            lbStreet5.Text = dt.Rows(0).Item("Location") & ""
            lbStreet4.Text = dt.Rows(0).Item("str_sup") & ""
            lbPostalCOde.Text = dt.Rows(0).Item("Postal_Code") & ""
            If (dt.Rows(0).Item("Booking_no") & "" <> "") Then
                dt = clsGet.Get_BookingOneRecord(dt.Rows(0).Item("Booking_no") & "").Tables(0)
                If (dt.Rows.Count > 0) Then
                    lbVesselName.Text = dt.Rows(0).Item("Vehicle_Text") & " BOOKING NO : " & dt.Rows(0).Item("Booking_no")
                End If
            End If
            dt = clsGet.Get_OtherService_Item(Ots_ID)
            If (dt.Rows.Count > 0) Then
                gridDetailTariff.DataSource = dt
                gridDetailTariff.DataBind()
            End If
        End If
    End Sub

    Private Sub gridRecord_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridRecord.RowDataBound
        If (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridRecord.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridRecord.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridRecord.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridRecord.PageCount.ToString()
            End If
        End If
    End Sub

    Private Sub gridRecord_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gridRecord.RowEditing

    End Sub

    Private Sub BtnClearDetail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnClearDetail.Click
        PanelRecordOther.Visible = True
        PanelDetail.Visible = False
    End Sub

    Private Sub gridDetailTariff_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridDetailTariff.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            If (e.Row.RowIndex = 0) Then
                ViewState("VAT") = 0
                ViewState("SUM") = 0
                ViewState("WHT") = 0
            End If
            Try
                e.Row.Cells(3).Text = FormatNumber(e.Row.Cells(3).Text, 2)
                e.Row.Cells(4).Text = FormatNumber(e.Row.Cells(4).Text, 2)
                e.Row.Cells(5).Text = FormatNumber(e.Row.Cells(5).Text, 2)

                Dim Vat As String = ""
                Dim dsConfig As DataSet = Session("Config")
                If (dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(1) = "00") Then
                    Vat = CInt(dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(0)).ToString()
                Else
                    Vat = dsConfig.Tables(0).Rows(0).Item("Vat").ToString()
                End If
                Dim dt As DataTable = clsGet.Get_MeterailWhithodingTax(e.Row.Cells(1).Text)
                If (dt.Rows.Count > 0) Then
                    If (dt.Rows(0).Item("MAT_WHT") & "" = "01") Or (dt.Rows(0).Item("MAT_WHT") & "" = "02") Or (dt.Rows(0).Item("MAT_WHT") & "" = "03") Then
                        '1%
                        ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(e.Row.Cells(5).Text) * 1) / 100)))
                    ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "04") Or (dt.Rows(0).Item("MAT_WHT") & "" = "05") Then
                        '3%
                        ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(e.Row.Cells(5).Text) * 3) / 100)))
                    ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "06") Or (dt.Rows(0).Item("MAT_WHT") & "" = "22") Then
                        '5%
                        ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(e.Row.Cells(5).Text) * 5) / 100)))
                    End If
                End If
                ViewState("SUM") = CDbl(ViewState("SUM")) + CDbl(e.Row.Cells(5).Text)
                ViewState("VAT") = (CDbl(ViewState("VAT")) + ((CDbl(e.Row.Cells(5).Text) * CDbl(Vat)) / 100))

                If Not (CheckBox1.Checked) Then
                    lbSumDetail.Text = FormatNumber(ViewState("SUM"), 2)
                    lbVatDetail.Text = FormatNumber(ViewState("VAT"), 2)
                    lbWTDetail.Text = "0"
                    lbNETDetail.Text = FormatNumber(CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))), 2)
                Else
                    lbSumDetail.Text = FormatNumber(ViewState("SUM"), 2)
                    lbVatDetail.Text = FormatNumber(ViewState("VAT"), 2)
                    lbWTDetail.Text = FormatNumber(ViewState("WHT"), 2)
                    lbNETDetail.Text = FormatNumber((CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))) - CDbl(ViewState("WHT"))), 2)
                End If
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If Not (CheckBox1.Checked) Then
            lbSumDetail.Text = FormatNumber(ViewState("SUM"), 2)
            lbVatDetail.Text = FormatNumber(ViewState("VAT"), 2)
            lbWTDetail.Text = "0"
            lbNETDetail.Text = FormatNumber(CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))), 2)
        Else
            lbSumDetail.Text = FormatNumber(ViewState("SUM"), 2)
            lbVatDetail.Text = FormatNumber(ViewState("VAT"), 2)
            lbWTDetail.Text = FormatNumber(ViewState("WHT"), 2)
            lbNETDetail.Text = FormatNumber((CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))) - CDbl(ViewState("WHT"))), 2)
        End If
    End Sub

    Private Sub BtnPrintDetail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnPrintDetail.Click
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet

        Dim dt As DataTable = CreatePrint()
        For i As Integer = 0 To gridDetailTariff.Rows.Count - 1
            Dim chk As CheckBox = CType(gridDetailTariff.Rows(i).FindControl("chk"), CheckBox)
            If chk.Checked Then
                Dim dr As DataRow = dt.NewRow
                dr.Item("No") = ""
                dr.Item("Date") = ""
                dr.Item("Bank") = ""
                dr.Item("Description") = gridDetailTariff.DataKeys(i).Values("Text_line") & ""
                dr.Item("Amount") = FormatNumber(gridDetailTariff.DataKeys(i).Values("total"), 2)
                dt.Rows.Add(dr)
                dt.AcceptChanges()
            End If
        Next
        Dim Text_AMOUNT As String = ConvertMoneyToText(lbNETDetail.Text.Replace(",", ""))
        dsReport.Tables.Add(dt.Copy)
        dsReport.AcceptChanges()
        ReportName = "Other_Service.rdlc"
        ' dsReport = New DataSet
        reportDataSource = "Other_Service"

        Dim Name As String = ""
        If (lbBIlltoDetail.Text.Trim = "") Then
            Name = lbSoldTodetail.Text.Split("-")(1)
        Else
            Name = lbBIlltoDetail.Text.Split("-")(1)
        End If
        dt = clsGet.Get_OtherService(ViewState("OTS_ID_PRINT"))

        Dim address As String = dt.Rows(0).Item("Street") & " " & dt.Rows(0).Item("Str_Sup") & " " & dt.Rows(0).Item("Location") & " " & dt.Rows(0).Item("City") & " " & dt.Rows(0).Item("Postal_Code")

        paraName = "Name;Address;Ship;Ots;Date;Time;No;Admis;Vat;WHT;TotalText;Total"
        paraValue = Name & ";" & address & ";" & lbVesselName.Text & ";" & ViewState("OTS_ID_PRINT") & ";" & dt.Rows(0).Item("dayIn") & ";" & dt.Rows(0).Item("TimeIn") & ";;" & lbSumDetail.Text & ";" & lbVatDetail.Text & ";" & lbWTDetail.Text & ";" & Text_AMOUNT & ";" & lbNETDetail.Text


        Orientation = True
        reportType = "pdf"

        Try
            urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
        Catch ex As Exception
            lbNET.Text = ex.Message
        End Try
    End Sub

    Private Function ConvertMoneyToText(ByVal Money As String) As String
        Dim Digit As String = Money.Split(".")(0)
        Dim SubMoney As String
        Try
            SubMoney = Money.Split(".")(1)
        Catch ex As Exception
            SubMoney = "00"
        End Try
        Dim COuntLength As Int16 = Digit.Length

        Dim TxtMoney As String = ""

        For i As Integer = 0 To COuntLength - 1
            If (COuntLength > 7) Then
                Select Case (Digit.Substring(0, 1).ToString())
                    Case "9" : TxtMoney &= "เก้า"
                    Case "8" : TxtMoney &= "แปด"
                    Case "7" : TxtMoney &= "เจ็ด"
                    Case "6" : TxtMoney &= "หก"
                    Case "5" : TxtMoney &= "ห้า"
                    Case "4" : TxtMoney &= "สี่"
                    Case "3" : TxtMoney &= "สาม"
                    Case "2" : TxtMoney &= "สอง"
                    Case "1" : TxtMoney &= "หนึ่ง"
                End Select

                Select Case (COuntLength.ToString)
                    Case "13" : TxtMoney &= "ล้านล้าน"
                    Case "12" : TxtMoney &= "แสนล้าน"
                    Case "11" : TxtMoney &= "หมื่นล้าน"
                    Case "10" : TxtMoney &= "พันล้าน"
                    Case "9" : TxtMoney &= "ร้อยล้าน"
                    Case "8" : TxtMoney &= "สิบล้าน"
                End Select
            Else
                Select Case (Digit.Substring(0, 1).ToString())
                    Case "9" : TxtMoney &= "เก้า"
                    Case "8" : TxtMoney &= "แปด"
                    Case "7" : TxtMoney &= "เจ็ด"
                    Case "6" : TxtMoney &= "หก"
                    Case "5" : TxtMoney &= "ห้า"
                    Case "4" : TxtMoney &= "สี่"
                    Case "3" : TxtMoney &= "สาม"
                    Case "2"
                        If (COuntLength.ToString() = "2") Then
                            TxtMoney &= "ยี่"
                        Else
                            TxtMoney &= "สอง"
                        End If

                    Case "1"
                        If (COuntLength.ToString() = "2") Then
                            TxtMoney &= "เอ็ด"
                        Else
                            TxtMoney &= "หนึ่ง"
                        End If
                End Select

                Select Case (COuntLength.ToString)
                    Case "7" : TxtMoney &= "ล้าน"
                    Case "6" : TxtMoney &= "แสน"
                    Case "5" : TxtMoney &= "หมื่น"
                    Case "4" : TxtMoney &= "พัน"
                    Case "3" : TxtMoney &= "ร้อย"
                    Case "2" : TxtMoney &= "สิบ"
                    Case "1" : TxtMoney &= "บาท"
                End Select
            End If
            Select Case (COuntLength.ToString())
                Case "13"
                    Digit = CInt(Digit) Mod 1000000000000
                Case "12"
                    Digit = CInt(Digit) Mod 100000000000
                Case "11"
                    Digit = CInt(Digit) Mod 10000000000
                Case "10"
                    Digit = CInt(Digit) Mod 1000000000
                Case "9"
                    Digit = CInt(Digit) Mod 100000000
                Case "8"
                    Digit = CInt(Digit) Mod 10000000
                Case "7"
                    Digit = CInt(Digit) Mod 1000000
                Case "6"
                    Digit = CInt(Digit) Mod 100000
                Case "5"
                    Digit = CInt(Digit) Mod 10000
                Case "4"
                    Digit = CInt(Digit) Mod 1000
                Case "3"
                    Digit = CInt(Digit) Mod 100
                Case "2"
                    Digit = CInt(Digit) Mod 10
                Case "1"
                    Digit = CInt(Digit) Mod 1
            End Select
            COuntLength -= 1
        Next
        If (SubMoney <> "00") Then
            If (SubMoney.Length = 2) Then
                Select Case (SubMoney.Substring(0, 1))
                    Case "9" : TxtMoney &= "เก้าสิบ"
                    Case "8" : TxtMoney &= "แปดสิบ"
                    Case "7" : TxtMoney &= "เจ็ดสิบ"
                    Case "6" : TxtMoney &= "หกสิบ"
                    Case "5" : TxtMoney &= "ห้าสิบ"
                    Case "4" : TxtMoney &= "สี่สิบ"
                    Case "3" : TxtMoney &= "สามสิบ"
                    Case "2" : TxtMoney &= "ยี่สิบ"
                    Case "1" : TxtMoney &= "สิบ"
                End Select
                Select Case (SubMoney.Substring(1, 1))
                    Case "9" : TxtMoney &= "เก้าสตางค์"
                    Case "8" : TxtMoney &= "แปดสตางค์"
                    Case "7" : TxtMoney &= "เจ็ดสตางค์"
                    Case "6" : TxtMoney &= "หกสตางค์"
                    Case "5" : TxtMoney &= "ห้าสตางค์"
                    Case "4" : TxtMoney &= "สี่สตางค์"
                    Case "3" : TxtMoney &= "สามสตางค์"
                    Case "2" : TxtMoney &= "สองสตางค์"
                    Case "1"
                        If (SubMoney.Length = 2) Then
                            If (SubMoney.Substring(0, 1) = "0") Then
                                TxtMoney &= "หนึ่งสตางค์"
                            Else
                                TxtMoney &= "เอ็ดสตางค์"
                            End If

                        Else
                            TxtMoney &= "หนึ่งสตางค์"
                        End If
                End Select
            Else
                Select Case (SubMoney.Substring(0, 1))
                    Case "9" : TxtMoney &= "เก้าสิบสตางค์"
                    Case "8" : TxtMoney &= "แปดสิบสตางค์"
                    Case "7" : TxtMoney &= "เจ็ดสิบสตางค์"
                    Case "6" : TxtMoney &= "หกสิบสตางค์"
                    Case "5" : TxtMoney &= "ห้าสิบสตางค์"
                    Case "4" : TxtMoney &= "สี่สิบสตางค์"
                    Case "3" : TxtMoney &= "สามสิบสตางค์"
                    Case "2" : TxtMoney &= "ยี่สิบสตางค์"
                    Case "1" : TxtMoney &= "สิบสตางค์"
                End Select
            End If
        End If
        Return TxtMoney
    End Function

    Private Function CreatePrint()
        Dim dt As New DataTable
        dt.Columns.Add("No", GetType(String))
        dt.Columns.Add("Date", GetType(String))
        dt.Columns.Add("Bank", GetType(String))
        dt.Columns.Add("Description", GetType(String))
        dt.Columns.Add("Amount", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function

    Public Sub chk_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        ViewState("VAT") = 0
        ViewState("SUM") = 0
        ViewState("WHT") = 0
        For i As Integer = 0 To gridDetailTariff.Rows.Count - 1
            Dim chk As CheckBox = CType(gridDetailTariff.Rows(i).FindControl("chk"), CheckBox)
            If (chk.Checked) Then
                Try
                    Dim Vat As String = ""
                    Dim dsConfig As DataSet = Session("Config")
                    If (dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(1) = "00") Then
                        Vat = CInt(dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(0)).ToString()
                    Else
                        Vat = dsConfig.Tables(0).Rows(0).Item("Vat").ToString()
                    End If
                    Dim dt As DataTable = clsGet.Get_MeterailWhithodingTax(gridDetailTariff.Rows(i).Cells(1).Text)
                    If (dt.Rows.Count > 0) Then
                        If (dt.Rows(0).Item("MAT_WHT") & "" = "01") Or (dt.Rows(0).Item("MAT_WHT") & "" = "02") Or (dt.Rows(0).Item("MAT_WHT") & "" = "03") Then
                            '1%
                            ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(gridDetailTariff.Rows(i).Cells(5).Text) * 1) / 100)))
                        ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "04") Or (dt.Rows(0).Item("MAT_WHT") & "" = "05") Then
                            '3%
                            ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(gridDetailTariff.Rows(i).Cells(5).Text) * 3) / 100)))
                        ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "06") Or (dt.Rows(0).Item("MAT_WHT") & "" = "22") Then
                            '5%
                            ViewState("WHT") = (CDbl(ViewState("WHT")) + CDbl(((CDbl(gridDetailTariff.Rows(i).Cells(5).Text) * 5) / 100)))
                        End If
                    End If
                    ViewState("SUM") = CDbl(ViewState("SUM")) + CDbl(gridDetailTariff.Rows(i).Cells(5).Text)
                    ViewState("VAT") = (CDbl(ViewState("VAT")) + ((CDbl(gridDetailTariff.Rows(i).Cells(5).Text) * CDbl(Vat)) / 100))

                Catch ex As Exception

                End Try
            End If

        Next
        If Not (CheckBox1.Checked) Then
            lbSumDetail.Text = FormatNumber(ViewState("SUM"), 2)
            lbVatDetail.Text = FormatNumber(ViewState("VAT"), 2)
            lbWTDetail.Text = "0"
            lbNETDetail.Text = FormatNumber(CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))), 2)
        Else
            lbSumDetail.Text = FormatNumber(ViewState("SUM"), 2)
            lbVatDetail.Text = FormatNumber(ViewState("VAT"), 2)
            lbWTDetail.Text = FormatNumber(ViewState("WHT"), 2)
            lbNETDetail.Text = FormatNumber((CDbl(ViewState("SUM") + CDbl(ViewState("VAT"))) - CDbl(ViewState("WHT"))), 2)
        End If
    End Sub

    Private Sub BtnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet

        Dim dt As DataTable = CreatePrint()
        For i As Integer = 0 To gridTariff.Rows.Count - 1
            Dim chk As CheckBox = CType(gridTariff.Rows(i).FindControl("chk"), CheckBox)
            If chk.Checked Then
                Dim dr As DataRow = dt.NewRow
                dr.Item("No") = ""
                dr.Item("Date") = ""
                dr.Item("Bank") = ""
                dr.Item("Description") = gridTariff.DataKeys(i).Values("Text_line") & ""
                dr.Item("Amount") = FormatNumber(gridTariff.Rows(i).Cells(5).Text)
                dt.Rows.Add(dr)
                dt.AcceptChanges()
            End If
        Next
        Dim Text_AMOUNT As String = ConvertMoneyToText(lbNET.Text.Replace(",", ""))
        dsReport.Tables.Add(dt.Copy)
        dsReport.AcceptChanges()
        ReportName = "Other_Service.rdlc"
        ' dsReport = New DataSet
        reportDataSource = "Other_Service"

        Dim Name As String = ""
        If (radioCustomer.SelectedValue = "I") Then
            If (lbBillTO.Text.Trim = "") Then
                Name = txtCustomer.Text.Split("-")(1)
            Else
                Name = lbBillTO.Text.Split("-")(1)
            End If
        Else
            Name = txtCustomerOneTime.Text
        End If

        dt = clsGet.Get_OtherService(ViewState("Ots_ID_NewOT"))

        Dim address As String = dt.Rows(0).Item("Street") & " " & dt.Rows(0).Item("Str_Sup") & " " & dt.Rows(0).Item("Location") & " " & dt.Rows(0).Item("City") & " " & dt.Rows(0).Item("Postal_Code")

        paraName = "Name;Address;Ship;Ots;Date;Time;No;Admis;Vat;WHT;TotalText;Total"
        paraValue = Name & ";" & address & ";" & txtBooking.Text & ";" & ViewState("Ots_ID_NewOT") & ";" & dt.Rows(0).Item("dayIn") & ";" & dt.Rows(0).Item("TimeIn") & ";;" & lbSum.Text & ";" & lbVat.Text & ";" & lbWHT.Text & ";" & Text_AMOUNT & ";" & lbNET.Text


        Orientation = True
        reportType = "pdf"

        Try
            urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
        Catch ex As Exception
            lbNET.Text = ex.Message
        End Try
    End Sub

    Private Sub btnTariff_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTariff.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " document.getElementById('ctl00_contentplaceholder1_TabPayment_TabOtherService_framePopup').src = '../FormPopup/FrmTariff_Popup.aspx';", True)
        ModalPopupExtender1.Show()
    End Sub

    Private Sub btnBooking_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBooking.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " document.getElementById('ctl00_contentplaceholder1_TabPayment_TabOtherService_iframeVessel').src = '../FormPopup/Vessel_Popup.aspx';", True)
        ModalPopupExtender2.Show()
    End Sub
End Class